<?php
require('includes/application_top.php');
//update product
 global $db;
 if(isset($_POST['ud_id']))
 {
  	$products_expiration_date = $_POST['ud_products_expiration_date'];
  	if($products_expiration_date != "")
  	{
  		$arr = explode('-', $products_expiration_date);
  		if(count($arr) < 2)
  			$arr = explode('/', $products_expiration_date);
  	
  		if(count($arr) == 2)
  		{
  			if(strlen($arr[0]) == 1) $arr[0] = "0$arr[0]";
  			if(strlen($arr[1]) == 2) $arr[1] = "20$arr[1]";
  			$products_expiration_date = "$arr[1]-$arr[0]-28";
  		}
  	
	  	$db->Execute("update " . TABLE_PRODUCTS . " 
	  				set products_quantity='" . addslashes($_POST['ud_products_quantity']) . "', 
	  				products_model='" . addslashes($_POST['ud_products_model']) . "', 
	  				products_price='" . addslashes($_POST['ud_products_price']) . "', 
	  				products_expiration_date='$products_expiration_date'
	  				where products_id='" . $_POST['ud_id'] . "'");
  	}
  	else
  		$db->Execute("update " . TABLE_PRODUCTS . "
  				set products_quantity='" . addslashes($_POST['ud_products_quantity']) . "',
  				products_model='" . addslashes($_POST['ud_products_model']) . "',
  				products_price='" . addslashes($_POST['ud_products_price']) . "',
  				products_expiration_date=null
  				where products_id='" . $_POST['ud_id'] . "'");
  	
 	$db->Execute("update " . TABLE_PRODUCTS_DESCRIPTION . "
  			set products_name='" . addslashes(utf8_decode($_POST['ud_products_name'])) . "'
  			where products_id='" . $_POST['ud_id'] . "' and language_id='" . $_SESSION['languages_id'] . "'");
        $movements_unit = (int)$_POST['ud_products_quantity'] - (int)$_POST['ud_hidden_products_quantity'];        
        if($movements_unit !== 0)
        {            
            $db->Execute("insert into stock_movements (products_id, movements_units, stock_after, admin_id, movements_date, movements_comment, movements_type)
                values (
                    '" . $_POST['ud_id'] . "', 
                    '" . $movements_unit . "',
                    '" . (int)$_POST['ud_products_quantity'] . "',
                    '" . $_SESSION['admin_id'] . "',
                    now(),
                    '" . addslashes($_POST['ud_products_movements_comment']) . "',
                    'update_products'
                )");            
        }
 }
echo("SUCCESS");

?>
